/**
* Copyright (c) 2013-2014, qtone Inc. All Rights Reserved.
*
* @author: tanghuaicheng
* Project Name: testuc
* $Id:   2017-01-11 10:54:07 $
*/
package cn.tang.tframe.common.base;

import java.io.Serializable;

public class BaseResult<T> implements Serializable {

	private static final long serialVersionUID = -7684836452637484092L;
	protected boolean success = true;
	protected String resultCode;
	protected String message;
	protected Throwable exception;
	private T model;
	protected long timestampMs = System.currentTimeMillis();

	public BaseResult() {
	}

	public static <T> BaseResult<T> success() {
		BaseResult<T> baseResult = new BaseResult<>();
		baseResult.setResultCode(ResultCode.SUCCESS.getCode());
		return baseResult;
	}

	public static <T> BaseResult<T> success(T model) {
		BaseResult<T> baseResult = new BaseResult<>();
		baseResult.setResultCode(ResultCode.SUCCESS.getCode());
		baseResult.setModel(model);
		return baseResult;
	}

	public static <T> BaseResult<T> fail(String message) {
		return fail(ResultCode.OTHER.getCode(), message, null);
	}

	public static <T> BaseResult<T> fail(String resultCode,String message) {
		return fail(resultCode,message,null);
	}

	public static <T> BaseResult<T> fail(String message, Throwable exception) {
		return fail(ResultCode.OTHER.getCode(),message,exception);
	}

	public static <T> BaseResult<T> fail(String resultCode, String message, Throwable exception) {
		BaseResult<T> baseResult = new BaseResult<>();
		baseResult.setSuccess(false);
		baseResult.setResultCode(resultCode);
		baseResult.setMessage(message);
		baseResult.setException(exception);
		return baseResult;
	}

	public boolean getSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getResultCode() {
		return resultCode;
	}

	public void setResultCode(String resultCode) {
		this.resultCode = resultCode;
	}

	public long getTimestampMs() {
		return timestampMs;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Throwable getException() {
		return exception;
	}

	public void setException(Throwable exception) {
		this.exception = exception;
	}

	public T getModel() {
		return model;
	}

	public void setModel(T model) {
		this.model = model;
	}

	@Override
	public String toString() {
		return "BaseResult{" + "success=" + success + ", resultCode=" + resultCode + ", message='" + message + '\''
				+ ", model=" + model + '}';
	}
}